import request from "@/utils/request";
import { getToken, getUserId, getSiteId } from "@/utils/auth";

// 订单管理查询
export function orderQueryApi(params = {
  keyStr: "",
  BeginDate: "",
  EndDate: "",
  IsFinish: -1,
  Urgent: -1,
  IsOverTime: -1,
  IsReturn: -1,
  ProcessId: -1,
  PageIndex: 1,
  PageSize: 10,
}) {
  return request({
    url: "/OrderV3Query/GetOrderList",
    method: "get",
    params: {
      "AccessToken": getToken(),
      "SiteId": getSiteId(),
      "KeyStr": params.keyStr || '',
      "BeginDate": params.BeginDate,
      "EndDate": params.EndDate,
      "IsFinish": params.IsFinish || -1,
      "Urgent": params.Urgent || -1,
      "IsOverTime": params.IsOverTime || -1,
      "IsReturn": params.IsReturn || -1,
      "ProcessId": params.ProcessId || -1,
      "PageIndex": params.PageIndex || 1,
      "PageSize": params.PageSize || 10
    },
  });
}

// 销售单查询，订单管理页面下半部分
export function saleOrderQueryApi(params = {
  "KeyStr": "",
  "BeginDate": "2025-03-01",
  "EndDate": "2025-03-31",
  "CodeType": -1,
  "PageIndex": 1,
  "PageSize": 1000
}) {
  return request({
    url: "/TOrderV3Query/GetTOrderList",
    method: "get",
    params: {
      "AccessToken": getToken(),
      "SiteId": getSiteId(),
      "KeyStr": params.keyStr || '',
      "BeginDate": params.BeginDate,
      "EndDate": params.EndDate,
      "CodeType": params.CodeType || -1,
      "PageIndex": params.PageIndex || 1,
      "PageSize": params.PageSize || 1000
    },
  });
}

// 销售单详情，订单管理页面下半部分
export function saleOrderDetailApi(orderId) {
  return request({
    url: "/TOrderV3Query/GetTOrderList",
    method: "get",
    params: {
      "AccessToken": getToken(),
      "SiteId": getSiteId(),
      TOrderId: orderId
    },
  });
}

// 新增销售单
export function saleOrderAddApi(params = {
  OrderCode: '',
  CustomerName: '',
  BrandName: '',
  CreateTime: '',
  OrderTime: '',
  SeriesName: '',
  OrderId: '',
  AttrList: []
}) {
  return request({
    url: "/OrderV3Add/Post",
    method: "post",
    data: {
      "AccessToken": getToken(),
      "SiteId": getSiteId(),
      OrderId: params.OrderId || 0,
      "OrderCode": params.OrderCode || '',
      "CustomerName": params.CustomerName || '',
      "BrandName": params.BrandName || '',
      "CreateTime": params.CreateTime || '',
      "OrderTime": params.OrderTime || '',
      "SeriesName": params.SeriesName || '',
      "AttrList": params.AttrList
    },
  });
}

// 新增销售单空单或修改总单信息
export function saleOrderAddEmptyApi(params = {
  TOrderCode: '',
  CustomerName: '',
  BrandName: '',
  TOrderEDate: '',
  SeriesName: '',
  TOrderId: '',
}) {
  return request({
    url: "/TOrderV3Add/Post",
    method: "post",
    data: {
      "AccessToken": getToken(),
      "SiteId": getSiteId(),
      TOrderId: params.TOrderId || 0,
      "TOrderCode": params.TOrderCode || '',
      "CustomerName": params.CustomerName || '',
      "BrandName": params.BrandName || '',
      "TOrderEDate": params.TOrderEDate || '',
      "SeriesName": params.SeriesName || '',
    },
  });
}

// 删除销售单
export function saleOrderRemoveApi(
  TOrderIds = []
) {
  return request({
    url: "/TOrderV3Delete/Post",
    method: "post",
    data: {
      "AccessToken": getToken(),
      "SiteId": getSiteId(),
      "TOrderIds": TOrderIds.join(',')
    },
  });
}

// 查询最晚创建的销售单编号
export function orderCodeApi(
) {
  return request({
    url: "/TOrderV3Query/GetLastTOrderCode",
    method: "get",
    params: {
      "AccessToken": getToken(),
      "SiteId": getSiteId(),
    },
  });
}

// 删除单条子单
export function orderRemoveApi(
  OrderIds
) {
  return request({
    url: "/OrderV3Delete/Post",
    method: "post",
    data: {
      "AccessToken": getToken(),
      "SiteId": getSiteId(),
      OrderIds
    },
  });
}

